package maosy.oauth2.password.oto;

import maosy.oauth2.password.entities.TbUserEntity;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class TBUserOTO implements UserDetails {

    private TbUserEntity userEntity;

    public TBUserOTO(TbUserEntity userEntity) {
        this.userEntity = userEntity;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<AuthorityOTO> list = new ArrayList<>();
        AuthorityOTO oto;
        if (this.userEntity.getUserType() == 2) {
            oto = new AuthorityOTO("ADMIN_USER");
        } else {
            oto = new AuthorityOTO("AGENT_USER");
        }
        list.add(oto);
        return list;
    }

    @Override
    public String getPassword() {
        return this.userEntity.getPassword();
    }

    @Override
    public String getUsername() {
        return this.userEntity.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.userEntity.getUserState() == 1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return !this.userEntity.getIsDelete();
    }
}
